package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.SingModel;
import com.hangzhou.xyj.model.SingPageInfoModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.GetSingByConditionHandler;
import com.hangzhou.xyj.net.handler.HotSingHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetSingByConditionReq;
import com.hangzhou.xyj.net.request.HotSingReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.SingingListAdapter;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.utils.StringUtil;
import com.hangzhou.xyj.utils.constants.Constants;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yaohuix on 2016/1/27.13:37
 * <p/>
 * 说明：唱戏->点戏->点戏台  热门点唱/分类点戏/名家点戏
 */
public class SingingListActivity extends BaseActivity {
    RecyclerView mRecyclerView;
    private String mTitle = "";
    private SingingListAdapter mAdapter;
    private int mIntentTag;
    private int mIndex;//索引

    @Override
    public void initIntentData(Intent intent) {
        mTitle = intent.getStringExtra(DataKey.TITLE);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, -1);
        if (StringUtil.isNotEmpty(intent.getStringExtra(DataKey.INDEX))) {
            mIndex = Integer.valueOf(intent.getStringExtra(DataKey.INDEX));
        }
        Log.d("debug", "SingingListActivity[mTitle=" + mTitle + ",mIntentTag=" + mIntentTag + ",mIndex=" + mIndex + "]");
    }


    @Override
    public void initDatas() {
        switch (EIntentTagType.valueOf(mIntentTag)) {
            case TAG_DIAN_DRAMA_HOT_SING:
                getHotSingList(1);
                break;
            case TAG_DIAN_DRAMA_EXPERT:
                getExpertSingListByUser(mIndex, 1);
                break;
            case TAG_DIAN_DRAMA_CATEGORY:
            default:
                getCatSingListByCatId(mIndex, 1);
                break;
        }
        mAdapter.setIntentTag(mIntentTag);
    }

    /**
     * 获取热门点唱列表
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/15 9:23
     */
    public void getHotSingList(final int currentPage) {
        //请求数据
        HotSingReq hotSingReq = new HotSingReq(Constants.PAGE_SIZE, currentPage);
        HttpLoader.getInstance().sendRequest(new HotSingHandler(hotSingReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                mAdapter.setIntentTag(EIntentTagType.TAG_DIAN_DRAMA_HOT_SING.getValue());
                List<SingModel> list = ((SingPageInfoModel) model).getSingList();
                if (currentPage == 1 && (list == null || list.isEmpty())) {
                    setNoDataTip(true, "暂时没有数据哦~");
                } else if (currentPage == 1) {
                    mAdapter.load(list);
                } else {
                    mAdapter.update(list);
                }
            }
            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }


    /**
     * 获取点戏-点戏台-名家点戏 某一个名家的列表
     *
     * @param userId 用户ID
     * @return
     * @author yaohuix
     * @date 2016/3/15 9:29
     */
    public void getExpertSingListByUser(int userId, final int currentPage) {
        GetSingByConditionReq getSingByConditionReq = new GetSingByConditionReq(GetSingByConditionReq.EParmType.TYPE_BY_USER_ID, userId,
                currentPage, Constants.PAGE_SIZE);
        HttpLoader.getInstance().sendRequest(new GetSingByConditionHandler(getSingByConditionReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                mAdapter.setIntentTag(EIntentTagType.TAG_DIAN_DRAMA_EXPERT.getValue());
                List<SingModel> list = ((SingPageInfoModel) model).getSingList();
                if (currentPage == 1 && (list == null || list.isEmpty())) {
                    setNoDataTip(true, "暂时没有数据哦~");
                } else if (currentPage == 1) {
                    mAdapter.load(list);
                } else {
                    mAdapter.update(list);
                }

            }
            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }

    /**
     * 获取点戏-点戏台-分类点戏 某一个剧种的唱戏列表
     *
     * @param catId 剧种的ID
     * @return
     * @author yaohuix
     * @date 2016/3/15 9:31
     */
    public void getCatSingListByCatId(int catId, final int currentPage) {
        GetSingByConditionReq getSingByConditionReq = new GetSingByConditionReq(GetSingByConditionReq.EParmType.TYPE_BY_CAT_ID, catId, currentPage, Constants.PAGE_SIZE);
        HttpLoader.getInstance().sendRequest(new GetSingByConditionHandler(getSingByConditionReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                mAdapter.setIntentTag(EIntentTagType.TAG_DIAN_DRAMA_CATEGORY.getValue());
                List<SingModel> list = ((SingPageInfoModel) model).getSingList();
                if (currentPage == 1 && (list == null || list.isEmpty())) {
                    setNoDataTip(true, "暂时没有数据哦~");
                } else if (currentPage == 1) {
                    mAdapter.load(list);
                } else {
                    mAdapter.update(list);
                }


            }
            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                mAdapter.setLoadingNoMore();//加载失败的时候
            }

            @Override
            public void onError() {
                super.onError();
                mAdapter.setLoadingNoMore();//加载失败的时候
            }
        }, 0));
    }


    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_hot_singing);
    }

    @Override
    public void initListeners() {
        mAdapter.setOnLoadingListener(new BaseLoadingAdapter.OnLoadingListener() {
            @Override
            public void loading() {
                mRecyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        switch (EIntentTagType.valueOf(mIntentTag)) {
                            case TAG_DIAN_DRAMA_HOT_SING:
                                getHotSingList(mAdapter.getCurrentPage());
                                break;
                            case TAG_DIAN_DRAMA_EXPERT:
                                getExpertSingListByUser(mIndex, mAdapter.getCurrentPage());
                                break;
                            case TAG_DIAN_DRAMA_CATEGORY:
                            default:
                                getCatSingListByCatId(mIndex, mAdapter.getCurrentPage());
                                break;
                        }
                    }
                }, 800);
            }
        });
    }

    @Override
    public void initTop() {
        enableTop(true);
        enableTitle(true, mTitle);
    }

    @Override
    public void initViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_content);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new SingingListAdapter(mRecyclerView, new ArrayList<SingModel>()));
    }
}
